import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class SelectImagePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return SelectImageState();
  }
}

class SelectImageState extends State<SelectImagePage> {
  var _imgPath;

  /*拍照*/
  _takePhoto() async {
    var image = await ImagePicker.pickImage(source: ImageSource.camera);
    setState(() {
      _imgPath = image;
    });
  }

  /*相册*/
  _openGallery() async {
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _imgPath = image;
    });
  }

  /*图片控件*/
  // ignore: non_constant_identifier_names
  Widget _ImageView(imgPath) {
    if (imgPath == null) {
      return Center(
        child: Text("请选择图片或拍照"),
      );
    } else {
      return Image.file(
        imgPath,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          _ImageView(_imgPath),
          MaterialButton(
            child: Text('选择照片'),
            onPressed: _openGallery,
          ),
          MaterialButton(
            child: Text('拍照'),
            onPressed: _takePhoto,
          ),
        ],
      ),
    );
  }
}
